<?php
global $_GPC, $_W;
$system=pdo_get('wxc_system',array('uniacid'=>$_W['uniacid']));
$GLOBALS['frames'] = $this->getMainMenu();
$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
$type=isset($_GPC['type'])?$_GPC['type']:'now';
$where=" where c.uniacid=:uniacid and c.id=:id";
$data[':uniacid']=$_W['uniacid'];
$data[':id']=$_GPC['id'];
if(isset($_GPC['keywords'])){
    $where.=" and (b.title LIKE  concat('%', :name,'%') || b.order_number LIKE  concat('%', :name,'%'))";
    $data[':name']=$_GPC['keywords']; 
    $type='all';  
}
if($_GPC['ids']){
    $res=pdo_delete("wxc_order",array('id'=>$_GPC['ids']));
    if($res){
        message('删除成功！', $this->createWebUrl('order'), 'success');
    }else{
        message('删除失败！','','error');
    }
}

if($_GPC['op']=='delete'){
    $order = pdo_get('wxc_order',array('id'=>$_GPC['id']));
//    $tasks = pdo_getall('wxc_task',array('order_id'=>$order['id']));
//    print_r(json_encode($tasks));die;
    $res=pdo_delete('wxc_order',array('id'=>$_GPC['id']));

    $res1=pdo_delete('wxc_task',array('order_id'=>$order['id']));
    if($res){
        message('删除成功！', $this->createWebUrl('mypartaked',array('id'=>$_GPC['id1'])), 'success');
    }else{
        message('删除失败！','','error');
    }
}


 if($type=='published'){
    $where.=" and b.state=1";
}
if($type=='finished'){
    $where.=" and b.state=2";
}

$sql="SELECT b.*,d.name,c.id as user_id FROM ".tablename('wxc_task'). " a"  . " left join " . tablename("wxc_order") . " b on a.order_id=b.id " . " left join " . tablename("wxc_users") . " c on a.u_id=c.id ". " left join " . tablename("wxc_category") . " d on b.c_id=d.id ".$where." ORDER BY b.created_time DESC";

$total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('wxc_task'). " a"  . " left join " . tablename("wxc_order") . " b on a.order_id=b.id  " . " left join " . tablename("wxc_users") . " c on a.u_id=c.id ". " left join " . tablename("wxc_category") . " d on b.c_id=d.id ".$where." ORDER BY b.created_time DESC",$data);

$select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
$list=pdo_fetchall($select_sql,$data);
$pager = pagination($total, $pageindex, $pagesize);
//print_r(json_encode($list));die;
// var_dump($list);die;
foreach ($list as $b){
    $end_time = strtotime($b['end_time']);
    if ($end_time < time() && $b['state']==1){
        $res=pdo_update('wxc_order',array('state'=>2),array('id'=>$b['id']));
    }if ($_GPC['op'] == 'change') {
          $res=pdo_update('wxc_order',array('status'=>$_GPC['status']),array('id'=>$_GPC['id']));
          if($res){
              message('操作成功',$this->createWebUrl('mypartaked',array()),'success');
          }else{
              message('操作失败','','error');
          }
      }
}

include $this->template('web/mypartaked');